var mprompt = {
	prefs : null,
	
	extra_option_elements : [],
	
	opener : function () {
		if (opener.wrappedJSObject) {
			return opener.wrappedJSObject;
		}
		
		return opener;
	},
	
	load : function () {
		var op = mprompt.opener();
		
		this.prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService)
			.getBranch("performancing.");
		
		document.getElementById("draft-checkbox").hidden = !op.pffEditorUI.parentWindow.SCRIBEFIRE_BLOG.supportsDrafts;
		document.getElementById("draft-checkbox").checked = op.pffEditorUI.isDraft || this.prefs.getBoolPref("publish.checkdraft");
		document.getElementById("custom-fields").hidden = !op.pffEditorUI.parentWindow.SCRIBEFIRE_BLOG.supportsCustomFields;
		
		if (op.pffEditorUI.parentWindow.SCRIBEFIRE_BLOG.supportsPages) {
			document.getElementById("publish-page").disabled = false;
		}
		
		if (op.pffEditorUI.entryId && op.pffEditorUI.type != 'note' && op.pffEditorUI.type != "") {
			document.getElementById("publish-edit").disabled = false;
			
			this.onEditSelect();
		}
		
		if (op.pffEditorUI.type == 'page') {
			document.getElementById("post-or-page").selectedIndex = 1;
		}
		
		if (window.arguments[1]) {
		    document.getElementById("slug").value = op.pffEditorUI.slug;
		}
		else {
			document.getElementById("slug-container").style.display = "none";
		}
		
		if (op.pffEditorUI.custom_fields.length > 0) {
			var model = document.getElementById("custom-fields").getElementsByTagName("row")[1];
			var fields = op.pffEditorUI.custom_fields;
			
			for (var i = 0; i < fields.length; i++) {
				var newRow = model.cloneNode(true);
				
				newRow.getElementsByTagName("textbox")[0].setAttribute("value", fields[i].key);
				newRow.getElementsByTagName("textbox")[1].setAttribute("value", fields[i].value);
				newRow.getElementsByTagName("textbox")[2].setAttribute("value", fields[i].id);
				
				model.parentNode.appendChild(newRow);
			}
			
			model.parentNode.removeChild(model);
			
			mprompt.toggleCustomFields();
		}
		
		var extra_options = window.arguments[2];
		
		if (extra_options) {
			for (var i in extra_options) {
				switch (extra_options[i].type) {
					case "checkbox":
						var el = document.createElement("checkbox");
						el.setAttribute("label", extra_options[i].label);
						el.setAttribute("checked", extra_options[i].checked);
						el.setAttribute("key", i);
						document.getElementById("extra_options").appendChild(el);
						
						mprompt.extra_option_elements.push(el);
					break;
					case "textbox":
						var label = document.createElement("label");
						label.setAttribute("value", extra_options[i].label);
						
						var tb = document.createElement("textbox");
						tb.setAttribute("key", i);
						
						var box = document.createElement("hbox");
						box.setAttribute("align", "center");
						
						box.appendChild(label);
						box.appendChild(tb);
						
						document.getElementById("extra_options").appendChild(box);
						
						mprompt.extra_option_elements.push(tb);
					break;
					case "file":
						var label = document.createElement("label");
						label.setAttribute("value", extra_options[i].label);
						
						var tb = document.createElement("textbox");
						tb.setAttribute("value", "");
						tb.setAttribute("key", i);
						
						var button = document.createElement("button");
						button.setAttribute("label", "Browse");
						button.setAttribute("oncommand", "mprompt.attachFile(this, this.lastSibling);");
						
						var box = document.createElement("hbox");
						box.setAttribute("align", "center");
						
						box.appendChild(label);
						box.appendChild(tb);
						box.appendChild(button);
						
						document.getElementById("extra_options").appendChild(box);
						
						mprompt.extra_option_elements.push(button);
					break;
				}
			}
		}
		
		sizeToContent();
	},
	
	attachFile : function (button, textbox) {
		var nsIFilePicker = Components.interfaces.nsIFilePicker;
		var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
		
		fp.init(window, "Select a file", nsIFilePicker.modeOpen);
		var res = fp.show();
		
		if (res == nsIFilePicker.returnOK){
			var data = "";
			
			var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"].
			                        createInstance(Components.interfaces.nsIFileInputStream);
			var cstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"].
			                        createInstance(Components.interfaces.nsIConverterInputStream);
			fstream.init(fp.file, -1, 0, 0);  
			cstream.init(fstream, "UTF-8", 0, 0);
			
			let (str = {}) {  
				cstream.readString(-1, str); // read the whole file and put it in str.value
				data = str.value;
			}  
			
			cstream.close(); // this closes fstream
			
			button.media = data;
			textbox.value = thefile.path;
		}
	},
	
	onEditSelect : function () {
		document.getElementById("edit-or-new").selectedIndex = 0;
		document.getElementById("publish-page").disabled = true;
		document.getElementById("publish-post").disabled = true;
	},
	
	onNewSelect : function () {
		document.getElementById("edit-or-new").selectedIndex = 1;
		document.getElementById("publish-page").disabled = false || !mprompt.opener().pffEditorUI.parentWindow.SCRIBEFIRE_BLOG.supportsPages;
		document.getElementById("publish-post").disabled = false;
	},
	
	cancel : function () {
		var re = window.arguments[0];
		re[0] = 0;
	},
	
	accept : function (val) {
		var val = 'post';
		
		if (document.getElementById("publish-edit").selected) {
			val = 'edit';
		}
		else {
			if (document.getElementById('publish-page').selected) {
				val = 'page';
			}
		}
		
		var re = window.arguments[0];
		re[0] = val;
		
		re[1] = document.getElementById("draft-checkbox").checked;
		re[2] = document.getElementById("slug").value;
		
		re[3] = [];
		
		var textfields = document.getElementById("custom-fields").getElementsByTagName("textbox");
		
		for (var i = 0; i < textfields.length; i += 3) {
			var field = {"key": textfields[i].value, "value": textfields[i+1].value };
			
			if (textfields[i+2].value != "") {
				field.id = textfields[i+2].value;
			}
			
			re[3].push(field);
		}
		
		re[4] = {};
		
		for (var i = 0; i < this.extra_option_elements.length; i++) {
			var el = this.extra_option_elements[i];
			var key = el.getAttribute("key");
			
			if (el.nodeName == "checkbox") {
				re[4][key] = el.checked;
			}
			else if (el.nodeName == "textbox") {
				re[4][key] = el.value;
			}
			else if (el.nodeName == "button") {
				re[4][key] = el.media;
			}
		}
		
		return true;
	},
	
	addCustomField : function (model) {
		var newRow = model.cloneNode(true);
		model.parentNode.appendChild(newRow);
		
		var t = newRow.getElementsByTagName("textbox");
		
		for (var i = 0; i < t.length; i++) {
			t[i].value = "";
		}
		
		sizeToContent();
	},
	
	toggleCustomFields : function () {
		var button = document.getElementById("toggle-custom-fields");
		
		if (button.className == "expander-down") {
			button.className = "expander-up";
			document.getElementById("custom-fields-inner").style.display = "";
		}
		else {
			button.className = "expander-down";
			document.getElementById("custom-fields-inner").style.display = "none";
		}
		
		sizeToContent();
	}
};